package com.py.credit.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.entity.Organization;
import com.py.credit.service.ModelBaseService;
import com.py.credit.service.OrgService;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.rest.support.RestServiceResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "model", description = "机构模型")
@RestController
@RequestMapping(value = "/model")
public class ModelController {

	@Autowired
	private OrgService orgService;

	@Autowired
	private ModelBaseService modelBaseService;

	@ApiOperation(value = "获取所有模型", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/getModelList.json", method = RequestMethod.GET)
	public RestServiceResult<List<Map<String, Object>>> getModelList(
			@ApiParam(name = "name", value = "模型名称", required = false) @RequestParam(required = false) String name) {

		return new RestServiceResult<>(modelBaseService.getModelList(name));

	}

	@ApiOperation(value = "分页获取模型", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/getModelListForPge.json")
	public RestServiceResult<List<Map<String, Object>>> getModelListForPge(
			@ApiParam(name = "name", value = "模型名称", required = false) @RequestParam(required = false) String name,
			@ApiParam(name = "orgId", value = "组织编号", required = false) @RequestParam(required = false) String orgId,
			Page page) {

		List<Map<String, Object>> modelListForPage = modelBaseService.getModelListForPage(name, page, orgId);

		return new RestServiceResult<>(modelListForPage);

	}

	@ApiOperation(value = "获取所有机构", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/getOrgList.json", method = RequestMethod.GET)
	public RestServiceResult<List<Organization>> getOrgList(
			@ApiParam(name = "name", value = "机构名称", required = false) @RequestParam(required = false) String name) {

		return new RestServiceResult<>(orgService.getOrgList(name));
	}

	@ApiOperation(value = "分页获取机构", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/getOrgListForPage.json")
	public RestServiceResult<List<Organization>> getOrgListForPage(
			@ApiParam(name = "orgName", value = "机构名称", required = false) @RequestParam(required = false) String orgName,
			@ApiParam(name = "modelId", value = "模型编号", required = false) @RequestParam(required = false) String modelId,
			Page page) {

		List<Organization> orgListForPage = orgService.getOrgListForPage(orgName, page, modelId);

		return new RestServiceResult<>(orgListForPage);
	}

	@ApiOperation(value = "模型绑定机构", httpMethod = "POST", produces = "application/json")
	@RequestMapping(value = "/addModelOrg.json", method = RequestMethod.POST)
	public RestServiceResult<String> addModleOrg(
			@ApiParam(name = "modelIds", value = "模型编号数组", required = true) @RequestParam(required = true) String[] modelIds,
			@ApiParam(name = "orgIds", value = "机构编号数组", required = true) @RequestParam(required = true) String[] orgIds,
			@ApiParam(name = "type", value = "类型", required = true) @RequestParam(required = true) String type) {

		orgService.addModelOrg(modelIds, orgIds, type);
		return new RestServiceResult<>();
	}

	@ApiOperation(value = "一键模型绑定机构", httpMethod = "POST", produces = "application/json")
	@RequestMapping(value = "/batchAddModelOrg.json", method = RequestMethod.POST)
	public RestServiceResult<String> batchAddModelOrg(
			@ApiParam(name = "modelIds", value = "模型编号数组", required = true) @RequestParam(required = true) String[] modelIds) {

		orgService.batchAddModelOrg(modelIds);
		return new RestServiceResult<>();
	}

	@ApiOperation(value = "一键机构绑定模型", httpMethod = "POST", produces = "application/json")
	@RequestMapping(value = "/batchAddOrgModel.json", method = RequestMethod.POST)
	public RestServiceResult<String> batchAddOrgModel(
			@ApiParam(name = "orgIds", value = "组织编号数组", required = true) @RequestParam(required = true) String[] orgIds) {

		orgService.batchAddOrgModel(orgIds);
		return new RestServiceResult<>();
	}

	@ApiOperation(value = "获取模型关联机构", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/getModelOrgList.json", method = RequestMethod.GET)
	public RestServiceResult<List<Map<String, Object>>> getModleOrgList(
			@ApiParam(name = "id", value = "模型编号", required = true) @RequestParam(required = true) String id,
			@ApiParam(name = "orgName", value = "机构名称", required = true) @RequestParam(required = true) String orgName,
			Page page) {

		List<Map<String, Object>> orgs = orgService.getOrgListByModelIdForPage(id, orgName, page);

		return new RestServiceResult<>(page.getTotalCount(), orgs);
	}

	@ApiOperation(value = "取消绑定模型关联机构", httpMethod = "POST", produces = "application/json")
	@RequestMapping(value = "/batchReleaseModelOrg.json", method = RequestMethod.POST)
	public RestServiceResult<String> batchReleaseModelOrg(
			@ApiParam(name = "modelIds", value = "模型编号数组", required = true) @RequestParam(required = true) String[] modelIds) {

		orgService.batchReleaseModelOrg(modelIds);
		return new RestServiceResult<>();
	}

	@ApiOperation(value = "取消绑定机构绑定模型", httpMethod = "POST", produces = "application/json")
	@RequestMapping(value = "/batchReleaseOrgModel.json", method = RequestMethod.POST)
	public RestServiceResult<String> batchReleaseOrgModel(
			@ApiParam(name = "orgIds", value = "组织编号数组", required = true) @RequestParam(required = true) String[] orgIds) {

		orgService.batchReleaseOrgModel(orgIds);
		return new RestServiceResult<>();
	}

	@ApiOperation(value = "单个取消绑定机构绑定模型", httpMethod = "POST", produces = "application/json")
	@RequestMapping(value = "/releaseOneOrgModel.json", method = RequestMethod.POST)
	public RestServiceResult<String> releaseOneOrgModel(
			@ApiParam(name = "orgId", value = "组织编号", required = true) @RequestParam(required = true) String orgId,
			@ApiParam(name = "modelIds", value = "模型编号数组", required = true) @RequestParam(required = true) String[] modelIds) {

		if (orgService.orgHasProcess(orgId, modelIds)) {

			throw new ServiceException("机构和模型存在评价流程无法取消绑定");
		}
		orgService.releaseOneOrgModel(orgId, modelIds);
		return new RestServiceResult<>();
	}

	@ApiOperation(value = "单个取消绑定模型绑定机构", httpMethod = "POST", produces = "application/json")
	@RequestMapping(value = "/releaseOneModelOrg.json", method = RequestMethod.POST)
	public RestServiceResult<String> releaseOneModelOrg(
			@ApiParam(name = "modelId", value = "组织编号", required = true) @RequestParam(required = true) String modelId,
			@ApiParam(name = "orgIds", value = "组织编号数据", required = true) @RequestParam(required = true) String[] orgIds) {

		if (orgService.modelHasProcess(modelId, orgIds)) {

			throw new ServiceException("机构和模型存在评价流程无法取消绑定");
		}
		orgService.releaseOneModelOrg(modelId, orgIds);
		return new RestServiceResult<>();
	}

	@ApiOperation(value = "获取机构关联模型列表", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/getOrgModelList.json", method = RequestMethod.GET)
	public RestServiceResult<List<Map<String, Object>>> getOrgModelList(
			@ApiParam(name = "id", value = "组织编号", required = true) @RequestParam(required = true) String id,
			@ApiParam(name = "modelName", value = "模型名称", required = true) @RequestParam(required = true) String modelName,
			Page page) {

		List<Map<String, Object>> models = orgService.getModelListByOrgIdForPage(id, modelName, page);

		return new RestServiceResult<>(page.getTotalCount(), models);
	}

}
